package zhang.lao.console.controller.wn;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import zhang.lao.annotation.RepeatSubmit;
import zhang.lao.build.tool.CookieKit;
import zhang.lao.build.tool.des.Des;
import zhang.lao.console.constant.ConsoleUserConstant;
import zhang.lao.pojo.console.req.BootStrapGridReq;
import zhang.lao.pojo.console.resp.BootStrapGridResp;
import zhang.lao.pojo.console.resp.HttpResult;
import zhang.lao.service.console.base.WnCompanyService;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
* 公司信息
* @author ZhangQin
* @create 2017年08月28日 17:02
**/
@Controller
public class WnCompanyController{
	@Resource
	private WnCompanyService wnCompanyService;

	@RequestMapping("/console/wn_company/add")
	@RepeatSubmit(isAdd = true)
	public String add(){
		return wnCompanyService.add();
	}

	@RepeatSubmit(isAdd = true)
	@RequestMapping("/console/wn_company/edit")
	public String edit(ModelMap modelMap,String id){
		return  wnCompanyService.edit(modelMap,id);
	}

	@RequestMapping("/console/wn_company/list")
	public String list(){
		return  wnCompanyService.list();
	}

	@RequestMapping("/console/wn_company/admin/choose")
	public String choose(ModelMap modelMap){

		return  wnCompanyService.choose(modelMap);
	}
	@RequestMapping("/console/wn_company/admin/choose/{companyId}")
	public String chooseCompanyId(@PathVariable String companyId, HttpServletResponse response){
		Cookie cookie = null;
		try {
			cookie = new Cookie(ConsoleUserConstant.COOKIE_COMPANY, Des.encode(ConsoleUserConstant.COOKIE_Company_DES_KEY,companyId));
			cookie.setMaxAge(-1);
			cookie.setPath("/");
			response.addCookie(cookie);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  "redirect:/console";
	}

	@RequestMapping("/console/wn_company/json")
	public @ResponseBody BootStrapGridResp json(BootStrapGridReq bootGridReq){
		return wnCompanyService.json(bootGridReq);
	}

	@RepeatSubmit(isAdd = false)
	@RequestMapping("/console/wn_company/save")
	public @ResponseBody HttpResult save(String formObjectJson){
		return wnCompanyService.save(formObjectJson);

	}

	@RequestMapping("/console/wn_company/delete")
	public @ResponseBody HttpResult delete(String ids){
        return wnCompanyService.delete(ids);
	}
}
